//
// Created by admin on 2022/6/10.
// 150. 逆波兰表达式求值: https://leetcode.cn/problems/evaluate-reverse-polish-notation/
//
#include <vector>
#include <string>
#include <stack>
using namespace std;

class EvalRPN {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> stk;
        for(auto& token: tokens) {
            if (isNumber(token)) {
                stk.push(atoi(token.c_str()));
            } else {
                int num2 = stk.top();
                stk.pop();
                int num1 = stk.top();
                stk.pop();
                switch (token[0]) {
                    case '+':
                        stk.push(num1 + num2);
                        break;
                    case '-':
                        stk.push(num1 - num2);
                        break;
                    case '*':
                        stk.push(num1 * num2);
                        break;
                    case '/':
                        stk.push(num1 / num2);
                        break;
                }
            }
        }
        return stk.top();
    }

private:
    bool isNumber(string& token) {
        return !(token == "+" || token == "-" || token == "*" || token == "/");
    }
};

